import { getPlayers } from "@/api/player.js";
import { getStore, setStore } from "@/utils/store.js";
const state = {
  account_id: getStore("account_id") || "",
  userInfo: getStore("userInfo") || {},
  infoTotal: getStore("infoTotal") || {},
  recentMatches: getStore("recentMatches") || []
};
const mutations = {
  SET_ACCOUNT_ID: (state, account_id) => {
    state.account_id = account_id;
    setStore("account_id", account_id);
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo;
    setStore("userInfo", userInfo);
  },
  SET_INFO_TOTAL: (state, infoTotal) => {
    state.infoTotal = infoTotal;
    setStore("infoTotal", infoTotal);
  },
  SET_RECENT_MATCHES: (state, recentMatches) => {
    state.recentMatches = recentMatches;
    setStore("recentMatches", recentMatches);
  }
};
const actions = {
  login({ commit }, account_id) {
    return new Promise((resolve, reject) => {
      getPlayers(account_id).then(res => {
        if (res.profile) {
          commit("SET_ACCOUNT_ID", account_id);
          commit("SET_USER_INFO", res);
          resolve(res.profile.personaname);
        } else {
          reject("请输入正确的数字ID");
        }
      });
    });
  },
  logout({ commit }) {
    commit("SET_ACCOUNT_ID", "");
  }
};

export default { state, mutations, actions };
